package com.mcptt.config.user.profile.sub.sub.sub;

import java.util.List;

import org.jdom.Element;
import org.jdom.JDOMException;

import com.cloudptt.api.product.log.Log;


public class EmergencyCall {
	public static final String Name = "EmergencyCall";
	public static final String Name_MCPTTPrivateRecipient = "MCPTTPrivateRecipient";
	public static final String Name_MCPTTGroupInitiation = "MCPTTGroupInitiation";

	private String type = "";//1:MCPTTPrivateRecipient 2:MCPTTGroupInitiation
	private Entry entry = null;
	private String privateRecipient = "";
	private String groupInitiation = "";

	public void parse(Element elem) throws JDOMException
	{
		if(elem == null)
		{
			return;
		}
		List<Element> list = elem.getChildren();//��ø��ڵ���ӽڵ�  
		for(Element subElem:list) 
		{
			String name = subElem.getName();
			Log.debug(this.getClass(), "name = " + name);

			if(name == null
					|| name.length() <= 0)
			{
				return;
			}
			if(name.equals(Name_MCPTTPrivateRecipient))
			{
				type = Name_MCPTTPrivateRecipient;
				parseSubElem(subElem);
			}
			else if(name.equals(Name_MCPTTGroupInitiation))
			{
				type = Name_MCPTTGroupInitiation;
				parseSubElem(subElem);
			}
		}  
	}

	public void parseSubElem(Element elem) throws JDOMException
	{
		if(elem == null)
		{
			return;
		}
		List<Element> list = elem.getChildren();//��ø��ڵ���ӽڵ�  
		for(Element subElem:list) 
		{
			String name = subElem.getName();
			Log.debug(this.getClass(), "name = " + name);

			if(name == null
					|| name.length() <= 0)
			{
				return;
			}

			if(name.equals(Entry.Name))
			{
				entry = new Entry();
				entry.parse(subElem);
			}
			if(Name_MCPTTPrivateRecipient.equals(type))
			{
				privateRecipient = entry.getUriEntry();
			}
			else if(Name_MCPTTGroupInitiation.equals(type))
			{
				groupInitiation = entry.getUriEntry();
			}
		}  
	}
	public String getPrivateRecipient() {
		return privateRecipient;
	}

	public String getGroupInitiation() {
		return groupInitiation;
	}
	public String toString()
	{
		StringBuffer strBuf = new StringBuffer("\r\n");
		strBuf.append("<").append(Name).append(">").append("\r\n");
		if(Name_MCPTTPrivateRecipient.equals(type))
		{
			strBuf.append("    ").append("<").append(Name_MCPTTPrivateRecipient).append(">").append("\r\n");
		}
		else if(Name_MCPTTGroupInitiation.equals(type))
		{
			strBuf.append("    ").append("<").append(Name_MCPTTGroupInitiation).append(">").append("\r\n");
		}

		strBuf.append(entry);

		if(Name_MCPTTPrivateRecipient.equals(type))
		{
			strBuf.append("    ").append("</").append(Name_MCPTTPrivateRecipient).append(">").append("\r\n");
		}
		else if(Name_MCPTTGroupInitiation.equals(type))
		{
			strBuf.append("    ").append("</").append(Name_MCPTTGroupInitiation).append(">").append("\r\n");
		}

		strBuf.append("</").append(Name).append(">").append("\r\n");

		return strBuf.toString();
	}
}
